package com.changpay.zywy.ui.fragment.mine

import android.os.Bundle
import android.support.v7.widget.DefaultItemAnimator
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.changpay.zywy.R
import com.changpay.zywy.adapter.IncomeDetailAdapter
import com.changpay.zywy.app.App
import com.changpay.zywy.base.BaseMvpFragment
import com.changpay.zywy.bean.EarningDetailInfo
import com.changpay.zywy.mvp.contract.IncomDetailContract
import com.changpay.zywy.mvp.presenter.IncomeDetailPresenter
import com.changpay.zywy.wight.CustomLoadMoreView
import com.changpay.zywy.wight.SpaceItemDecoration
import kotlinx.android.synthetic.main.fragment_refresh_layout.*


class IncomeDetailFragment : BaseMvpFragment<IncomDetailContract.View, IncomDetailContract.Presenter>(),
    IncomDetailContract.View {
    override fun showEarningsDetailList(data: List<EarningDetailInfo>) {
        data.let {
            mAdapter.run {
                addData(it)
                val size = it.size
                if (size < 10) {
                    loadMoreEnd(true)
                } else {
                    loadMoreComplete()
                }
            }
        }
    }

    private var page = 1
    private val pagesize = "10"


    private var position: Int = 0  // 当前viewPage的位置
    private lateinit var type: String
    private lateinit var startTime: String
    private lateinit var endTime: String
    private lateinit var wallet_type: String
    private lateinit var gc_id: String


    companion object {
        fun getInstance(instanceType: Int, startTime: String, endTime: String,wallet_type:String,gc_id:String): IncomeDetailFragment {
            val bundle = Bundle()
            bundle.putInt("position", instanceType)
            bundle.putString("startTime", startTime)
            bundle.putString("endTime", endTime)
            bundle.putString("wallet_type", wallet_type)
            bundle.putString("gc_id", gc_id)
            val fragment = IncomeDetailFragment()
            fragment.arguments = bundle
            return fragment
        }
    }


    override fun createPresenter(): IncomDetailContract.Presenter = IncomeDetailPresenter()

    override fun attachLayoutRes(): Int = R.layout.fragment_refresh_layout

    override fun lazyLoad() {
        loadData()
    }

    fun loadData() {
        val param = hashMapOf<String, String>()
        param["key"] = token
        param["page"] = page.toString()
        param["pagesize"] = pagesize
//        param["search_date_star"] = startTime
        param["search_date_end"] = endTime
        param["wallet_type"] = wallet_type
        param["type"] = type
        if(gc_id != "-1") param["gc_id"] = gc_id
        mPresenter?.getEarningsDetailList(param)
    }

    override fun initView(view: View) {
        super.initView(view)
        position = arguments?.getInt("position") ?: 0
        startTime = arguments?.getString("startTime").toString()
        endTime = arguments?.getString("endTime").toString()
        wallet_type = arguments?.getString("wallet_type").toString()
        gc_id = arguments?.getString("gc_id").toString()
        when (position) {
            0 -> type = "partner"
            1 -> type = "merchant"
            2 -> type = "activation"
            3 -> type = "lineCard_full"
            4 -> type = "cash_pay"
            5 -> type = "other"
            6 -> type = "reapy"
        }
        swipeRefreshLayout.isEnabled = false
        initRecyclerView()
    }

    /**
     * datas
     */
    private val datas = mutableListOf<EarningDetailInfo>()

    /**
     * Adapter
     */
    private val mAdapter: IncomeDetailAdapter by lazy {
        IncomeDetailAdapter(context, datas)
    }

    /**
     * LinearLayoutManager
     */
    private val linearLayoutManager: LinearLayoutManager by lazy {
        LinearLayoutManager(context)
    }

    /**
     * LoadMoreListener
     */
    private val onRequestLoadMoreListener = BaseQuickAdapter.RequestLoadMoreListener {
        page = mAdapter.data.size / 10 + 1
        loadData()

    }

    fun reLoadData()
    {
        page = 1
        datas.clear()
        loadData()
    }


    /**
     * ItemClickListener
     */
    private val onItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->

    }

    /**
     * RecyclerView Divider
     */
    private val recyclerViewItemDecoration by lazy {
        this.let {
            SpaceItemDecoration(App.context!!)
        }
    }

    /**
     * ItemChildClickListener
     */
    private val onItemChildClickListener = BaseQuickAdapter.OnItemChildClickListener { _, view, position ->

    }


    private fun initRecyclerView() {
        recyclerView.run {
            layoutManager = linearLayoutManager
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
            recyclerViewItemDecoration.let { addItemDecoration(it) }
        }
        mAdapter.run {
            bindToRecyclerView(recyclerView)
            setOnLoadMoreListener(onRequestLoadMoreListener, recyclerView)
            setLoadMoreView(CustomLoadMoreView())
            onItemClickListener = this@IncomeDetailFragment.onItemClickListener
            setEmptyView(R.layout.fragment_empty_layout)
        }


    }

    fun setEndTime(endTime: String)
    {
        this.endTime = endTime
    }

    fun setGcid(gc_id: String)
    {
        this.gc_id = gc_id
    }
}






